<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="租赁人" prop="leaseholder">
              <el-input v-model="queryParams.leaseholder" placeholder="请输入租赁人" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="联系方式" prop="contactInformation">
              <el-input v-model="queryParams.contactInformation" placeholder="请输入联系方式" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="项目名称" prop="projectName">
              <el-input v-model="queryParams.projectName" placeholder="请输入项目名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="楼&nbsp;&nbsp;栋&nbsp;&nbsp;号" prop="towerNumber">
              <el-input v-model="queryParams.towerNumber" placeholder="请输入楼栋号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="房&nbsp;&nbsp;间&nbsp;&nbsp;号" prop="roomNumber">
              <el-input v-model="queryParams.roomNumber" placeholder="请输入房间号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="退租类型" prop="rentOutType">
              <el-select v-model="queryParams.rentOutType" placeholder="请选择退租类型" clearable>
                <el-option v-for="dict in park_surrender_tenancy_status" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <div style="display: flex; justify-content: flex-end; align-items: center">
          <el-button type="warning" icon="Download" @click="handleExport"> 导出 </el-button>
          <!-- <el-button plain icon="Plus" @click="handleAdd">新增</el-button> -->
          <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()">删除</el-button>
        </div>
      </template>

      <el-table v-loading="loading" :data="flathistoryRentalList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column v-if="false" label="主键" align="center" prop="id" />
        <el-table-column v-if="false" label="公寓基础信息主键" align="center" prop="apartmentBasicInfoId" />
        <el-table-column v-if="false" label="公寓租赁主键" align="center" prop="apartmentRentalInfoId" />
        <el-table-column label="租赁人" align="center" prop="leaseholder" />
        <el-table-column label="联系方式" align="center" prop="contactInformation" />
        <el-table-column label="开始时间" align="center" prop="startTime" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="结束时间" align="center" prop="endTime" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="退租时间" align="center" prop="rentOutTime" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.rentOutTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="项目名称" align="center" prop="projectName" />
        <el-table-column label="楼栋号" align="center" prop="towerNumber" />
        <el-table-column label="所在楼层" align="center" prop="floor" />
        <el-table-column label="房间号" align="center" prop="roomNumber" />
        <el-table-column label="建筑面积(㎡)" align="center" prop="floorArea" />
        <el-table-column label="退租类型" align="center" prop="rentOutType">
          <template #default="scope">
            <dict-tag :options="park_surrender_tenancy_status" :value="scope.row.rentOutType" />
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120" fixed="right" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="详情" placement="top">
              <el-button link type="primary" @click="handleView(scope.row)">详情</el-button>
            </el-tooltip>
            <el-tooltip content="详情" placement="top">
              <!-- <el-button link type="primary" @click="handleView(scope.row)">编辑</el-button> -->
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" :total="total" @pagination="getList" />
    </el-card>
    <!-- 添加或修改公寓历史租赁对话框 -->
    <!-- <el-dialog v-model="dialog.visible" :title="dialog.title" width="500px" append-to-body>
      <el-form ref="flathistoryRentalFormRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="公寓信息" prop="apartmentBasicInfoId">
          <el-input v-model="form.apartmentBasicInfoId" placeholder="请输入公寓基础信息" />
        </el-form-item>
        <el-form-item label="公寓租赁" prop="apartmentRentalInfoId">
          <el-input v-model="form.apartmentRentalInfoId" placeholder="请输入公寓租赁" />
        </el-form-item>
        <el-form-item label="租赁人" prop="leaseholder">
          <el-input v-model="form.leaseholder" placeholder="请输入租赁人" />
        </el-form-item>
        <el-form-item label="联系方式" prop="contactInformation">
          <el-input v-model="form.contactInformation" placeholder="请输入联系方式" />
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-date-picker v-model="form.startTime" clearable type="date" value-format="YYYY-MM-DD" placeholder="请选择开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker v-model="form.endTime" clearable type="date" value-format="YYYY-MM-DD" placeholder="请选择结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="退租时间" prop="rentOutTime">
          <el-date-picker v-model="form.rentOutTime" clearable type="date" value-format="YYYY-MM-DD" placeholder="请选择退租时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="项目名称" prop="projectName">
          <el-input v-model="form.projectName" placeholder="请输入项目名称" />
        </el-form-item>
        <el-form-item label="楼&nbsp;&nbsp;栋&nbsp;&nbsp;号" prop="towerNumber">
          <el-input v-model="form.towerNumber" placeholder="请输入楼栋号" />
        </el-form-item>
        <el-form-item label="所在楼层" prop="floor">
          <el-input v-model="form.floor" placeholder="请输入所在楼层" />
        </el-form-item>
        <el-form-item label="房&nbsp;&nbsp;间&nbsp;&nbsp;号" prop="roomNumber">
          <el-input v-model="form.roomNumber" placeholder="请输入房间号" />
        </el-form-item>
        <el-form-item label="建筑面积(㎡)" prop="floorArea">
          <el-input v-model="form.floorArea" placeholder="请输入建筑面积" />
        </el-form-item>
        <el-form-item label="退租类型" prop="rentOutType">
          <el-select v-model="form.rentOutType" placeholder="请选择退租类型">
            <el-option v-for="dict in park_surrender_tenancy_status" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="创建时间" prop="creatTime">
          <el-date-picker v-model="form.creatTime" clearable type="date" value-format="YYYY-MM-DD" placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog> -->
    <FlatFormDialog ref="flatFormDialogRef" @refresh="getList" />
  </div>
</template>

<script setup name="FlathistoryRental" lang="ts">
import FlatFormDialog from '@/components/FlatFormDialog/index.vue';
import {
  listFlathistoryRental,
  getFlathistoryRental,
  delFlathistoryRental,
  addFlathistoryRental,
  updateFlathistoryRental
} from '@/api/zichanguanli/flathistoryRental';
import { FlathistoryRentalVO, FlathistoryRentalQuery, FlathistoryRentalForm } from '@/api/zichanguanli/flathistoryRental/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { park_surrender_tenancy_status } = toRefs<any>(proxy?.useDict('park_surrender_tenancy_status'));

const flathistoryRentalList = ref<FlathistoryRentalVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const flathistoryRentalFormRef = ref<ElFormInstance>();
const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const flatFormDialogRef = ref(null);
const flatFormDialogOpen = (type: string, id?: string | number) => {
  flatFormDialogRef.value.open(type, id);
};

const initFormData: FlathistoryRentalForm = {
  id: undefined,
  apartmentBasicInfoId: undefined,
  apartmentRentalInfoId: undefined,
  leaseholder: undefined,
  contactInformation: undefined,
  startTime: undefined,
  endTime: undefined,
  rentOutTime: undefined,
  projectName: undefined,
  towerNumber: undefined,
  floor: undefined,
  roomNumber: undefined,
  floorArea: undefined,
  rentOutType: undefined,
  creatTime: undefined
};
const data = reactive<PageData<FlathistoryRentalForm, FlathistoryRentalQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    leaseholder: undefined,
    contactInformation: undefined,
    projectName: undefined,
    towerNumber: undefined,
    roomNumber: undefined,
    rentOutType: undefined,
    params: {}
  },
  rules: {
    id: [{ required: true, message: '主键不能为空', trigger: 'blur' }],
    apartmentBasicInfoId: [{ required: true, message: '公寓信息不能为空', trigger: 'blur' }],
    apartmentRentalInfoId: [{ required: true, message: '公寓租赁不能为空', trigger: 'blur' }],
    leaseholder: [{ required: true, message: '租赁人不能为空', trigger: 'blur' }],
    contactInformation: [
      { required: true, message: '联系方式不能为空', trigger: 'blur' },
      {  required: true, message: '请填写正确的手机号码', trigger: 'change' }
    ],
    startTime: [{ required: true, message: '开始时间不能为空', trigger: 'blur' }],
    endTime: [{ required: true, message: '结束时间不能为空', trigger: 'blur' }],
    rentOutTime: [{ required: true, message: '退租时间不能为空', trigger: 'blur' }],
    projectName: [{ required: true, message: '项目名称不能为空', trigger: 'blur' }],
    towerNumber: [{ required: true, message: '楼栋号不能为空', trigger: 'blur' }],
    floor: [{ required: true, message: '所在楼层不能为空', trigger: 'blur' }],
    roomNumber: [{ required: true, message: '房间号不能为空', trigger: 'blur' }],
    floorArea: [{ required: true, message: '建筑面积不能为空', trigger: 'blur' }],
    rentOutType: [{ required: true, message: '退租类型不能为空', trigger: 'change' }],
    creatTime: [{ required: true, message: '创建时间不能为空', trigger: 'blur' }]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询公寓历史租赁列表 */
const getList = async () => {
  loading.value = true;
  const res = await listFlathistoryRental(queryParams.value);
  flathistoryRentalList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
};

/** 表单重置 */
const reset = () => {
  form.value = { ...initFormData };
  flathistoryRentalFormRef.value?.resetFields();
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

/** 多选框选中数据 */
const handleSelectionChange = (selection: FlathistoryRentalVO[]) => {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};

/** 新增按钮操作 */
const handleAdd = () => {
  flatFormDialogOpen('create');
  // reset();
  // dialog.visible = true;
  // dialog.title = '添加公寓历史租赁';
};

/** 修改按钮操作 */
const handleUpdate = async (row?: FlathistoryRentalVO) => {
  const _id = row?.id || ids.value[0];
  flatFormDialogOpen('edit', _id);
};

/** 提交按钮 */
const submitForm = () => {
  flathistoryRentalFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id) {
        await updateFlathistoryRental(form.value).finally(() => (buttonLoading.value = false));
      } else {
        await addFlathistoryRental(form.value).finally(() => (buttonLoading.value = false));
      }
      proxy?.$modal.msgSuccess('操作成功');
      dialog.visible = false;
      await getList();
    }
  });
};

/** 删除按钮操作 */
const handleDelete = async (row?: FlathistoryRentalVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('确定删除所选的信息吗？').finally(() => (loading.value = false));
  await delFlathistoryRental(_ids);
  proxy?.$modal.msgSuccess('删除成功');
  await getList();
};

const handleView = (row) => {
  const _id = row?.id || ids.value[0];
  flatFormDialogOpen('history', _id);
};

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download(
    'zichanguanli/flathistoryRental/export',
    {
      ...queryParams.value
    },
    `flathistoryRental_${new Date().getTime()}.xlsx`
  );
};

onMounted(() => {
  getList();
});
</script>
